package com.google.code.mochaccino.framework.hibernate.provider.configuration.helper;
/*
 * Copyright 2012 Claude Houle claude.houle@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Properties;
import java.util.UUID;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory;
import org.hibernate.cache.internal.StandardQueryCacheFactory;
import org.hibernate.cache.spi.QueryCacheFactory;
import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.cfg.Environment;

/** Hibernate Cache Configurator. */
public final class ConfigurationHelperCache implements ConfigurationHelper {
	/** Concurrency Strategy */
	private CacheConcurrencyStrategy concurrencyStrategy = CacheConcurrencyStrategy.READ_WRITE;

	/** Query Cache Factory */
	private Class<? extends QueryCacheFactory> queryCacheFactoryClass = StandardQueryCacheFactory.class;

	/** Region Factory */
	private Class<? extends RegionFactory> cacheRegionFactory = SingletonEhCacheRegionFactory.class;

	/** Cache Region Prefix */
	private String cacheRegionPrefix = UUID.randomUUID().toString();

	/** Use Minimal Puts */
	private boolean cacheUseMinimalPuts = true;

	/** Use 2nd Level Cache */
	private boolean use2ndLevelCache = true;

	/** Use Query Cache */
	private boolean useQueryCache = true;

	/** Use Structure Cache (Human-Readable) */
	private boolean useStructuredCache = false;

	/** Ehcache Resource Name */
	private String ehcacheResourceName = "ehcache-hibernate-standalone.xml";

	@Override
	public void configure( Properties p ) {
		p.setProperty( Environment.CACHE_REGION_FACTORY, cacheRegionFactory.getName() );
		p.setProperty( Environment.CACHE_REGION_PREFIX, cacheRegionPrefix );
		p.setProperty( Environment.USE_QUERY_CACHE, Boolean.toString( useQueryCache ) );
		p.setProperty( Environment.QUERY_CACHE_FACTORY, queryCacheFactoryClass.getName() );
		p.setProperty( Environment.USE_SECOND_LEVEL_CACHE, Boolean.toString( use2ndLevelCache ) );
		p.setProperty( Environment.USE_MINIMAL_PUTS, String.valueOf( cacheUseMinimalPuts ) );
		p.setProperty( Environment.USE_STRUCTURED_CACHE, String.valueOf( useStructuredCache ) );
		p.setProperty( Environment.DEFAULT_CACHE_CONCURRENCY_STRATEGY, concurrencyStrategy.name() );
		p.setProperty( SingletonEhCacheRegionFactory.NET_SF_EHCACHE_CONFIGURATION_RESOURCE_NAME, ehcacheResourceName );
	}

	public void setCacheRegionFactory( Class<? extends RegionFactory> cacheRegionFactory ) {
		this.cacheRegionFactory = cacheRegionFactory;
	}

	public void setCacheRegionPrefix( String cacheRegionPrefix ) {
		this.cacheRegionPrefix = cacheRegionPrefix;
	}

	public void setCacheUseMinimalPuts( boolean cacheUseMinimalPuts ) {
		this.cacheUseMinimalPuts = cacheUseMinimalPuts;
	}

	public void setConcurrencyStrategy( CacheConcurrencyStrategy concurrencyStrategy ) {
		this.concurrencyStrategy = concurrencyStrategy;
	}

	public void setQueryCacheFactoryClass( Class<? extends QueryCacheFactory> queryCacheFactoryClass ) {
		this.queryCacheFactoryClass = queryCacheFactoryClass;
	}

	public void setUse2ndLevelCache( boolean use2ndLevelCache ) {
		this.use2ndLevelCache = use2ndLevelCache;
	}

	public void setUseQueryCache( boolean useQueryCache ) {
		this.useQueryCache = useQueryCache;
	}

	public void setUseStructuredCache( boolean useStructuredCache ) {
		this.useStructuredCache = useStructuredCache;
	}
}
